import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import React, { useState, useRef, forwardRef, useImperativeHandle, } from "react";
import { DevPopup } from "~/public/cpas-ui";
import { TextBox, TreeList } from "devextreme-react";
import AntTabs, { HocVerticalLeftStyleTab, } from "~/public/cpas-ui/components-ui/ant-components/AntTab";
import { Addsubject, AssistAdd, AssistEdit, AssistgetType, AutoAddChildrenForFZZ, autoAddFirstChildren, Editsubject, getAccountInfo, getNewKMBH, getSubjectSDB, getSubjectType, IsCanAddFZZ, SubjectDel, SubjectIsHasFZZ, } from "./DraftApi";
import { DevToolBar } from "~/public/cpas-ui";
import { Item } from "devextreme-react/toolbar";
import { Column } from "devextreme-react/tree-list";
import "./_style.less";
import notify from "devextreme/ui/notify";
import { CpasIcon } from "~/public/cpas-ui";
import { moneyFormat } from "../CarryEntryPopup/components";
import SubjectsPopupData from "../components/SubjectsPopupData";
import FirstLevelSubjectPopup from "../components/FirstLevelSubjectPopup";
/**
 * ## 基于本地数据库，获取数据本地数据进行功能演示。
 * ~~~js
 * import SubjectPopup  from 'sright-ui'
 * ~~~
 */
export const SubjectPopup = forwardRef((props, ref) => {
    console.log("执行组件--------");
    const diaParams = useRef();
    const [visible, setVisible] = useState(false);
    const [tabsData, setTabsData] = useState([]);
    const [SubjectSDBData, setSubjectSDBData] = useState([]);
    const [SubjectSDBDatas, setSubjectSDBDatas] = useState([]);
    const [selectMode, setSelectMode] = useState("");
    const [recursiveMultiple, setRecursiveMultiple] = useState(true);
    const [TbsStates, setTbsStates] = useState("0");
    const [AddFZZKMMC, setIsCanAddFZZKMMC] = useState("");
    const [curRowKMMC, setCurRowKMMC] = useState("");
    const [value, setValue] = useState("");
    const selectRowData = useRef();
    const TreeListRef = useRef();
    const ParentLevel = useRef();
    const curNodeRef = useRef();
    // 删除科目
    const DeleteSubjectRef = useRef();
    // 增加科目
    const SubjectPopupDataRef = useRef();
    const AuxiliaryPopupRef = useRef();
    const TreeAuxiliaryRef = useRef();
    const typeRef = useRef();
    const typeErrorRef = useRef();
    const FirstLevelSubjectPopupRef = useRef();
    // 选中行状态
    const [selectState, setSelectState] = useState(true);
    // 获取切换数据
    const getSubjectTypeData = async () => {
        const res = await getSubjectType({
            dbname: diaParams.current?.dbname,
            Data_ID: diaParams.current?.Data_ID,
        });
        if (res.success) {
            res.data.unshift({ code: "0", type: "全部" });
            setTabsData(res.data);
        }
        else {
            setTabsData([]);
        }
    };
    const tableScrollTo0 = () => {
        const leftTreeViewScrollView = TreeListRef?.current?.instance?.getScrollable();
        if (leftTreeViewScrollView) {
            // leftTreeViewScrollView.scrollTop();
            setValue("");
            TreeListRef.current.instance.searchByText();
            TreeListRef?.current?.instance?.clearSelection();
            leftTreeViewScrollView.scrollTo({ left: 0, top: 0 });
        }
    };
    // 获取表格数据
    const getSubjectSDBDatas = async () => {
        const res = await getSubjectSDB({
            dbname: diaParams.current?.dbname,
            Data_ID: diaParams.current?.Data_ID,
            isHasFZZ: diaParams.current?.isHasFZZ,
        });
        if (res.success) {
            setSubjectSDBData(res.data);
            setSubjectSDBDatas(res.data);
        }
        else {
            setSubjectSDBData([]);
            setSubjectSDBDatas([]);
        }
        tableScrollTo0();
    };
    // 自定义方法
    useImperativeHandle(ref, () => ({
        show: async (params) => {
            diaParams.current = params;
            setVisible(true);
            setSelectState(true);
            setSelectMode(diaParams.current?.selectMode);
            setRecursiveMultiple(diaParams.current?.recursiveMultiple ?? true);
            getSubjectSDBDatas();
            getSubjectTypeData();
        },
    }));
    const reloadTableAndTas = () => {
        getSubjectSDBDatas();
        setTbsStates("0");
        TreeListRef.current.instance.searchByText();
        TreeListRef.current.instance.clearSelection();
    };
    // 标签页过滤表格数据
    const getSubjectListData = async (val) => {
        if (val === "全部") {
            if (SubjectSDBData?.length)
                setSubjectSDBDatas(SubjectSDBData);
        }
        else {
            if (SubjectSDBData?.length) {
                const KMLXData = SubjectSDBData.filter((item) => {
                    return item.KMLX && item.KMLX.includes(val);
                });
                setSubjectSDBDatas(KMLXData);
            }
        }
        tableScrollTo0();
    };
    const AccountTabsClick = (val) => {
        setTbsStates(val);
        const resF = tabsData.filter((item) => val === item.code);
        getSubjectListData(resF[0]?.type ?? "全部");
    };
    const onCellClick = (e) => {
        console.log(e.data, selectMode, "@@@");
        if (selectMode === "0") {
            selectRowData.current = e.data;
        }
        else {
            const v = TreeListRef.current.instance.getSelectedRowsData("all");
            selectRowData.current = v;
        }
    };
    const GetautoAddFirstChildren = async (data) => {
        console.log(data, "-----");
        let paramsObj = {
            dbname: diaParams.current?.dbname,
            Data_ID: diaParams.current?.Data_ID,
            editUser: diaParams.current?.editUser,
        };
        if (selectRowData.current?.PID === "0") {
            paramsObj = {
                ...paramsObj,
                KMBH: selectRowData.current.KMBH,
            };
        }
        else {
            paramsObj = {
                ...paramsObj,
                KMBH: data.KMBH,
            };
        }
        await autoAddFirstChildren(paramsObj);
        const res = await getNewKMBH(paramsObj);
        if (res.ok) {
            diaParams.current?.okCallback(data, res.data);
        }
    };
    // // 判断是否可以增加辅助账
    async function IsCanAddFZZStates_okCallback(v, KMBH) {
        if (v[0].code === 1) {
            setVisible(false);
            const res = await AssistgetType({
                dbname: diaParams.current?.dbname,
                Data_ID: diaParams.current?.Data_ID,
                KMBH: KMBH,
            });
            console.log(selectRowData.current, res, "selectRowData.current, r------");
            if (res.ok) {
                // 后期会在调一个 自动生成挂辅助账的科目名称 传给第一个参数
                diaParams.current?.okCallback(selectRowData.current, res.data);
            }
        }
    }
    const AddFZZ_fun = async (rowData) => {
        const value = await AutoAddChildrenForFZZ({
            dbname: diaParams.current?.dbname,
            Data_ID: diaParams.current?.Data_ID,
            KMBH: rowData.KMBH,
            editUser: diaParams.current?.editUser,
        });
        const v = await AssistgetType({
            dbname: diaParams.current?.dbname,
            Data_ID: diaParams.current?.Data_ID,
            KMBH: value[0].KMBH,
        });
        console.log(value, "value------");
        diaParams.current?.okCallback(value, v.data);
        setVisible(false);
    };
    const okCallback_function = async (rowData) => {
        if (!diaParams.current?.okCallback)
            return notify("传入的okCallback出错", "error", 2000);
        if (!rowData)
            return notify("请选择您要选择的科目", "warning", 2000);
        setCurRowKMMC(rowData.KMMC);
        //判断 是否为增加辅助账 ture 还是 增加科目 false
        if (diaParams.current?.auxiliaryState) {
            const IsCanAddFZZState = await IsCanAddFZZ({
                dbname: diaParams.current?.dbname,
                Data_ID: diaParams.current?.Data_ID,
                KMBH: rowData.KMBH,
            });
            // 判断 能否增加辅助帐 1、直接增加 2、是否继续 0、非末级不可增加
            if (IsCanAddFZZState[0].code === 1) {
                // // 判断 是否存在辅助账
                const IsHasFZZ = await SubjectIsHasFZZ({
                    dbname: diaParams.current?.dbname,
                    Data_ID: diaParams.current?.Data_ID,
                    KMBH: rowData.KMBH,
                });
                if (IsHasFZZ[0].isExists) {
                    // 存在辅助账 判断是否可以增加辅助账
                    IsCanAddFZZStates_okCallback(IsCanAddFZZState, rowData.KMBH);
                }
                else {
                    AddFZZ_fun(rowData);
                }
            }
            else if (IsCanAddFZZState[0].code === 2) {
                setIsCanAddFZZKMMC(IsCanAddFZZState[0].message);
                TreeAuxiliaryRef.current.show({
                    title: "提示",
                    okCallback: async () => {
                        AddFZZ_fun(rowData);
                    },
                });
            }
            else if (IsCanAddFZZState[0].code === 0) {
                notify(`${IsCanAddFZZState[0].message}`, "warning", 2000);
            }
        }
        else {
            // 是否可以选择上级科目
            if (diaParams.current?.isSelectSuperior) {
                // 科目 判断 是否为末级
                if (rowData?.isEnd) {
                    typeRef.current.show({
                        title: "提示",
                        okCallback: () => {
                            setVisible(false);
                            GetautoAddFirstChildren(rowData);
                        },
                    });
                }
                else {
                    // 如果不是末级 直接返回参数 和关闭弹出框
                    setVisible(false);
                    const res = await getNewKMBH({
                        dbname: diaParams.current?.dbname,
                        Data_ID: diaParams.current?.Data_ID,
                        KMBH: rowData.KMBH,
                    });
                    if (res.ok) {
                        diaParams.current?.okCallback(rowData, res.data);
                    }
                }
            }
            else {
                if (selectMode === "1") {
                    const getrowdata = TreeListRef.current.instance.getSelectedRowsData();
                    diaParams.current?.okCallback(rowData, getrowdata);
                    setVisible(false);
                }
                else {
                    // 科目 判断 是否为末级
                    if (rowData?.isEnd) {
                        setVisible(false);
                        diaParams.current?.okCallback(rowData, "");
                    }
                    else {
                        notify("非末级结点科目,无法选中!", "error", 2000);
                    }
                }
            }
        }
        // setValue("");
        // setTbsStates("0");
        // TreeListRef.current.instance.searchByText();
        // TreeListRef.current.instance.clearSelection();
    };
    // 双击事件
    const onCellDblClick = async (e) => {
        const rowData = e.data;
        selectRowData.current = e.data;
        await okCallback_function(rowData);
    };
    // 按钮 确定事件
    const ok = async () => {
        const getrowdata = TreeListRef.current.instance.getSelectedRowsData();
        selectRowData.current = getrowdata[0];
        await okCallback_function(selectRowData.current);
    };
    function handleStr(str) {
        var arr = str.replace(/[`:_.~!@#$%^&*() \+ =<>?"{}|, \/ ;' \\ [ \] ·~！@#￥%……&*（）—— \+ ={}|《》？：“”【】、；‘’，。、]/g, "");
        return arr;
    }
    const saveAddsubject = async (tempObj, isAddMoreYear) => {
        const { dbname, Data_ID, editUser } = diaParams.current;
        if (!isAddMoreYear) {
            const res = await Addsubject(tempObj);
            if (res.ok) {
                notify("增加科目成功", "success", 2000);
                reloadTableAndTas();
            }
            return;
        }
        const resYear = await getAccountInfo({ dbname });
        if (resYear.success) {
            const yearData = resYear.data.map((item) => item.Data_ID);
            const tempA = [];
            for (let index = 0; index < yearData.length; index++) {
                const element = yearData[index];
                if (element) {
                    const res = await Addsubject({ ...tempObj, Data_ID: element });
                    if (res.ok && res.data > 0) {
                        tempA.push(Promise.resolve("ok"));
                    }
                    else {
                        tempA.push(Promise.resolve(`${element}年:${res.data}`));
                    }
                }
            }
            Promise.allSettled(tempA)
                .then((results) => {
                console.log(tempA, results, "@@@");
                reloadTableAndTas();
                const falseData = results
                    .filter((item1) => item1.value !== "ok")
                    .map((item) => item.value);
                if (falseData.length) {
                    // notify(`增加科目失败，失败原因为【${falseData.join(';')};】`, falseData.length === yearData.length ? 'error' : 'warning', 2000);
                    notify(`增加科目失败，失败原因为【${falseData.join(";")};】`, "warning", 4000);
                }
                else {
                    notify("增加科目成功", "success", 2000);
                }
            })
                .catch((error) => {
                // 处理错误
                console.error("An error occurred:", error);
            });
        }
        else {
            notify(`获取年度失败！`, "error", 2000);
        }
    };
    const saveAssistAdd = async (dataObj, isAddMoreYear) => {
        const { dbname, Data_ID, editUser } = diaParams.current;
        if (!isAddMoreYear) {
            const data = await AssistAdd(dataObj);
            if (data.ok) {
                notify("增加辅助账成功", "success", 2000);
                reloadTableAndTas();
            }
            else {
                notify("该科目下已存在相同名称的辅助核算项目，无法重复新增！", "error", 4000);
            }
            return;
        }
        const resYear = await getAccountInfo({ dbname });
        if (resYear.success) {
            const yearData = resYear.data.map((item) => item.Data_ID);
            const tempA = [];
            for (let index = 0; index < yearData.length; index++) {
                const element = yearData[index];
                if (element) {
                    const res = await AssistAdd({ ...dataObj, Data_ID: element });
                    if (res.ok && res.data > 0) {
                        tempA.push(Promise.resolve("ok"));
                    }
                    else {
                        tempA.push(Promise.resolve(`${element}年:${res.data}`));
                    }
                }
            }
            Promise.allSettled(tempA)
                .then((results) => {
                console.log(tempA, results, "@@@");
                reloadTableAndTas();
                const falseData = results
                    .filter((item1) => item1.value !== "ok")
                    .map((item) => item.value);
                if (falseData.length) {
                    // notify(`增加科目失败，失败原因为【${falseData.join(';')};】`, falseData.length === yearData.length ? 'error' : 'warning', 2000);
                    notify(`增加科目失败，失败原因为【${falseData.join(";")};】`, "warning", 2000);
                }
                else {
                    notify("增加辅助账成功", "success", 2000);
                }
            })
                .catch((error) => {
                // 处理错误
                console.error("An error occurred:", error);
            });
        }
        else {
            notify(`获取年度失败！`, "error", 2000);
        }
    };
    const menuAddsubjectFunc = async (rowData) => {
        const { dbname, Data_ID, editUser } = diaParams.current;
        const MatchKMMC = rowData.KMMC.match(/(\S*)-/);
        console.log(rowData, MatchKMMC, "@@@ MatchKMMC");
        const indexss = rowData.KMMC.lastIndexOf(`-${rowData.YSKMMC}`);
        const resolvess = rowData.KMMC.substring(0, indexss);
        const ItemDataPid = handleStr(rowData.PID);
        ParentLevel.current = ItemDataPid + "-" + resolvess;
        console.log(ParentLevel.current, resolvess, ItemDataPid, rowData.KMMC, "@@@ resolvess");
        let tabsArray = [...tabsData];
        tabsArray.shift();
        if (rowData.JC === 1) {
            SubjectPopupDataRef.current.show({
                args: {
                    title: "新增科目",
                    KMBH: "",
                    data: tabsArray,
                    ParentLevel: "",
                    ParentLevelKmbh: "",
                    PKMBH: "",
                    resolvess: rowData.KMMC,
                    datatKMFX: rowData.KMFX,
                    dataKMLX: rowData.KMLX,
                    isEdit: false,
                },
                okCallback: (r) => {
                    console.log(r, "@@@ 新增科目");
                    if (r) {
                        const itemNames = r.selectedYears || "";
                        let tempObj = {
                            dbname,
                            Data_ID: Data_ID,
                            editUser,
                            YSKMBH: `${r.KMBHAdd}`,
                            YSKMMC: r.SubjectNumber,
                            KMFX: r.SelectName,
                            KMLX: r.SelectType,
                            itemNames: itemNames,
                            KMBH: r.KMBH ? r.KMBH : r.KMBHAdd + "~|",
                        };
                        if (r.Satatejc) {
                            tempObj = {
                                ...tempObj,
                                KMMC: r.KMMC + "-" + r.SubjectNumber,
                                PKMBH: r.PKMBH,
                                JC: Number(r.Satatejc) + 1,
                            };
                        }
                        else {
                            tempObj = {
                                ...tempObj,
                                KMMC: r.SubjectNumber,
                                PKMBH: "",
                                JC: rowData.JC,
                            };
                        }
                        console.log(r, tempObj, "@@@ 这是新增科目tempObj");
                        saveAddsubject(tempObj, r.checkBoxState);
                    }
                },
                cancelCallback: () => { },
            });
            return;
        }
        console.log(rowData, "data----");
        const RNewKMBH = await getNewKMBH({
            dbname,
            Data_ID: Data_ID,
            KMBH: rowData.PID,
        });
        SubjectPopupDataRef.current.show({
            args: {
                title: "新增科目",
                KMBH: RNewKMBH.data[0].KMBH,
                data: tabsArray,
                ParentLevel: ParentLevel.current,
                ParentLevelKmbh: RNewKMBH.data[0].YSKMBH,
                PKMBH: rowData.PKMBH,
                resolvess,
                datatKMFX: rowData.KMFX,
                dataKMLX: rowData.KMLX,
                isEdit: false,
            },
            okCallback: (r) => {
                console.log(r, "------");
                // 修改 变动
                const itemNames = r.selectedYears || "";
                let dataObj = {};
                // 如果 他的上级科目被删除了 执行下方方法
                if (r.PKMBHState) {
                    dataObj = {
                        dbname,
                        Data_ID: Data_ID,
                        KMBH: r.KMBH ? r.KMBH : r.KMBHAdd + "~|",
                        KMMC: r.SubjectNumber,
                        editUser,
                        PKMBH: "",
                        YSKMBH: `${r.KMBHAdd}`,
                        YSKMMC: r.SubjectNumber,
                        KMFX: r.SelectName,
                        KMLX: r.SelectType,
                        JC: 1,
                        itemNames: itemNames,
                    };
                }
                else {
                    dataObj = {
                        dbname,
                        Data_ID: Data_ID,
                        KMBH: r.KMBH ? r.KMBH : RNewKMBH.data[0].KMBH,
                        KMMC: `${r.KMMC ? r.KMMC : resolvess}-${r.SubjectNumber}`,
                        editUser,
                        PKMBH: r.PKMBH,
                        YSKMBH: `${r.KMBHAdd}`,
                        YSKMMC: r.SubjectNumber,
                        KMFX: r.SelectName,
                        KMLX: r.SelectType,
                        JC: r.Satatejc ? Number(r.Satatejc) + 1 : rowData.JC,
                        itemNames: itemNames,
                    };
                }
                console.log(r, dataObj, "@@@");
                // debugger;
                saveAddsubject(dataObj, r.checkBoxState);
            },
            cancelCallback: () => { },
        });
        return;
    };
    const openAuxiliaryPopup = (tmepArgs, tmepBH, tempRowData) => {
        const { dbname, Data_ID, editUser } = diaParams.current;
        let tempObj = {
            dbname,
            Data_ID: Data_ID,
            editUser,
        };
        AuxiliaryPopupRef.current.show({
            args: tmepArgs,
            okCallback: (v) => {
                tempObj = {
                    ...tempObj,
                    KMBH: v.KMBH ? v.KMBH : tempRowData.KMBH,
                    KMMC: v.KMMC ? v.KMMC : tempRowData.KMMC,
                    HSXMLXBH: v.SelectHSXMLXBH,
                    YSKMBH: `${v.YSKMBH}` ? `${v.YSKMBH}` : `${tmepBH}`,
                    HSXMLXMC: v.selectValue,
                    HSXMBH: v.SelectHSXMLXBH + "-" + v.projectValueMds + "~|",
                    HSXMMC: v.projectValues,
                    YSHSXMMC: v.projectValues,
                    YSHSXMBH: v.SelectHSXMLXBH + "-" + v.projectValueMds,
                };
                saveAssistAdd(tempObj, v.checkBoxState);
            },
            cancelCallback: () => { },
        });
    };
    const menuEditsubjectFunc = async (rowData) => {
        const { dbname, Data_ID, editUser } = diaParams.current;
        const YSKMBH = handleStr(rowData.KMBH);
        const YSHSXMBH = handleStr(rowData.code);
        const indexss = rowData.KMMC.lastIndexOf(`-${rowData.YSKMMC}`);
        const resolvess = rowData.KMMC.substring(0, indexss);
        const ItemDataPid = handleStr(rowData.PID);
        ParentLevel.current = ItemDataPid + "-" + resolvess;
        let tabsArray = [...tabsData];
        tabsArray.shift();
        if (rowData.ZWType === 1) {
            const res = await AssistgetType({
                dbname,
                Data_ID: Data_ID,
                KMBH: rowData.KMBH,
            });
            // 判断 是否为 66为辅助类型 99辅助账
            if (rowData.level === 66) {
                return openAuxiliaryPopup({
                    title: "新增辅助账",
                    value: YSKMBH + "-" + rowData.KMMC,
                    auxiliaryState: true,
                    items: res.data,
                    edit: false,
                    fzzData: rowData,
                }, YSKMBH, rowData);
            }
            console.log(rowData, "curNodeRef.current.KMBH---");
            if (rowData.isNewAdd) {
                return AuxiliaryPopupRef.current.show({
                    args: {
                        title: "编辑辅助账",
                        value: YSKMBH + "-" + rowData.KMMC,
                        auxiliaryState: true,
                        items: res.data,
                        edit: true,
                        HSXMMC: rowData.HSXMMC,
                        fzzData: rowData,
                    },
                    okCallback: async (v) => {
                        const data = await AssistEdit({
                            dbname,
                            Data_ID: Data_ID,
                            KMBH: rowData.KMBH,
                            KMMC: rowData.KMMC,
                            editUser,
                            HSXMLXBH: v.SelectHSXMLXBH,
                            YSKMBH: `${YSKMBH}`,
                            HSXMLXMC: v.selectValue,
                            HSXMBH: rowData.code + "~|",
                            HSXMMC: v.projectValue,
                            YSHSXMMC: v.projectValue,
                            YSHSXMBH: YSHSXMBH,
                        });
                        if (data) {
                            notify("修改辅助账成功", "success", 2000);
                            reloadTableAndTas();
                        }
                    },
                    cancelCallback: () => { },
                });
            }
            notify(`辅助账【${rowData.HSXMMC}】为企业原始辅助账,不可编辑`, "warning", 2000);
            return;
        }
        else {
            console.log(rowData, "111111");
            // 编辑 科目
            if (rowData.isNewAdd) {
                console.log(rowData, "@@@ 编辑 科目");
                SubjectPopupDataRef.current.show({
                    args: {
                        title: "编辑",
                        KMBH: rowData.KMBH,
                        data: tabsArray,
                        ParentLevel: rowData.JC === 1 ? "" : ParentLevel.current,
                        ParentLevelKmbh: Number(rowData?.YSKMBH || 0),
                        PKMBH: rowData.PKMBH,
                        KMMC: rowData.YSKMMC,
                        datatKMFX: rowData.KMFX,
                        dataKMLX: rowData.KMLX,
                        itemNames: rowData.itemNames,
                        resolvess,
                        isEdit: true,
                    },
                    okCallback: async (v) => {
                        console.log(resolvess, "resolvess-----");
                        console.log("确定回调函数");
                        const itemNames = v.selectedYears || rowData.itemNames;
                        const tempKMMC = `${resolvess && resolvess + "-"}${v.SubjectNumber}`;
                        const r = await Editsubject({
                            dbname,
                            Data_ID: Data_ID,
                            KMBH: rowData.KMBH,
                            KMMC: tempKMMC,
                            editUser,
                            YSKMMC: v.SubjectNumber,
                            KMFX: v.SelectName,
                            KMLX: v.SelectType,
                            itemNames: itemNames,
                        });
                        if (r.ok) {
                            notify("编辑科目成功", "success", 2000);
                            reloadTableAndTas();
                        }
                    },
                    cancelCallback: () => { },
                });
            }
            else {
                notify(`科目【${rowData.KMMC}】为企业原始科目,不可编辑`, "warning", 2000);
            }
        }
    };
    const firstLevelSubjectFunc = () => {
        const { dbname, Data_ID, editUser } = diaParams.current;
        FirstLevelSubjectPopupRef.current.show({
            args: {
                title: "快速新增一级科目",
                selectYear: Data_ID,
                dbname,
                username: editUser,
                zcbfID: dbname,
            },
            okCallback: () => {
                reloadTableAndTas();
            },
        });
    };
    const relationMenuItems = (e) => {
        if (!TreeListRef.current)
            return;
        if (e.target == "content" && e.row?.rowType == "data") {
            TreeListRef.current?.instance.selectRows(e.row.key);
            const rowData = e.row?.data;
            curNodeRef.current = rowData;
            setCurRowKMMC(rowData.KMMC);
            console.log(diaParams.current, "@@@ !!!");
            e.items = [
                {
                    text: "快速新增一级科目",
                    onItemClick: () => {
                        firstLevelSubjectFunc();
                    },
                },
                {
                    text: "新增科目",
                    onItemClick: async (e) => {
                        menuAddsubjectFunc(rowData);
                    },
                },
                {
                    text: "编辑",
                    onItemClick: (e) => {
                        menuEditsubjectFunc(rowData);
                    },
                },
                {
                    text: "删除",
                    onItemClick: (e) => {
                        DeleteSubjectRef.current.show({
                            title: "删除提示",
                            okCallback: async () => {
                                // 删除 科目表
                                const res = await SubjectDel({
                                    dbname: diaParams.current?.dbname,
                                    Data_ID: diaParams.current?.Data_ID,
                                    editUser: diaParams.current?.editUser,
                                    KMBH: curNodeRef.current.KMBH,
                                });
                                if (res.ok) {
                                    notify("删除科目成功", "success", 2000);
                                    reloadTableAndTas();
                                }
                                else {
                                    notify(`${res.data}`, "warning", 2000);
                                }
                            },
                        });
                    },
                },
            ];
        }
    };
    // 模糊搜索
    const inputSearch = (content) => {
        TreeListRef.current.instance.searchByText(content || "");
    };
    // const throttleSearch = throttle(inputSearch, 100);
    const getInputValue = (e) => {
        const { event } = e;
        inputSearch(event.target.value);
        setValue(event.target.value);
    };
    const closePopup = () => {
        setValue("");
        setVisible(false);
        setTbsStates("0");
        TreeListRef.current.instance.searchByText();
        TreeListRef.current.instance.clearSelection();
    };
    return (_jsxs(_Fragment, { children: [_jsx(DevPopup.Logic, { ref: ref, width: 1000, height: 700, visible: visible, title: diaParams.current?.title, onOK: ok, onCancel: () => {
                    closePopup();
                }, onHiding: () => {
                    closePopup();
                }, children: () => {
                    return (_jsxs("div", { style: { width: "100%", height: "100%" }, children: [_jsx("div", { style: { display: "flex" }, children: _jsxs(DevToolBar.Default, { className: "ItemStyle", children: [_jsx(Item, { location: "before", children: _jsx("div", { style: {
                                                    display: "flex",
                                                    alignItems: "center",
                                                    marginLeft: "65px",
                                                }, children: _jsx("span", { style: { color: "red" }, children: "\u63D0\u793A\uFF1A\u53CC\u51FB\u53EF\u9009\u4E2D\u76F8\u5E94\u79D1\u76EE" }) }) }), _jsx(Item, { location: "after", children: _jsxs("div", { style: {
                                                    display: "flex",
                                                    alignItems: "center",
                                                    marginBottom: "10px",
                                                    marginLeft: "20px",
                                                }, children: [_jsx("span", { style: { marginRight: "15px" }, children: _jsx(TextBox, { onInput: (e) => getInputValue(e), width: 200, value: value, className: "TextBoxButtons_Style", buttons: [
                                                                {
                                                                    location: "before",
                                                                    name: "search",
                                                                    options: {
                                                                        icon: "iconfont icon-search",
                                                                        disabled: true,
                                                                    },
                                                                },
                                                            ] }) }), _jsx("span", { className: "refresh_Style", children: _jsx(CpasIcon, { fontSize: 15, type: "icon-refresh", onClick: () => {
                                                                getSubjectTypeData();
                                                                reloadTableAndTas();
                                                            } }) })] }) })] }) }), _jsxs("div", { style: { display: "flex", height: "92%" }, children: [_jsx("div", { style: { border: "1px solid #ddd", borderRight: "none" }, children: _jsx(HocVerticalLeftStyleTab, { activeKey: TbsStates, className: "SubjectsTabs", tabPosition: "left", tabBarStyle: { background: "#FFF" }, onChange: AccountTabsClick, children: Array.isArray(tabsData)
                                                ? tabsData.map((item) => {
                                                    return (_jsx(AntTabs.Panel, { tab: item.type, style: { marginTop: "5px" } }, item.code));
                                                })
                                                : "" }) }), _jsxs(TreeList, { selection: {
                                            recursive: recursiveMultiple,
                                            mode: selectMode === "0" ? "single" : "multiple",
                                        }, className: "PublicTreeList devHeadersCenter", ref: TreeListRef, dataSource: SubjectSDBDatas, allowColumnResizing: true, showBorders: true, showRowLines: true, 
                                        // allowColumnResizing={true}
                                        // columnAutoWidth={true}
                                        // showRowLines={true}
                                        // showBorders={true}
                                        columnResizingMode: "widget", columnMinWidth: 100, parentIdExpr: "PID", keyExpr: "ID", id: "Code", width: "95%", height: "100%", onCellClick: onCellClick, 
                                        // onContentReady={contentReady}
                                        onCellDblClick: onCellDblClick, onSelectionChanged: () => {
                                            console.log("触发----------");
                                            setSelectState(false);
                                        }, 
                                        // 显示斑马线
                                        rowAlternationEnabled: true, onContextMenuPreparing: relationMenuItems, children: [_jsx(Column, { dataField: "code", caption: "\u79D1\u76EE\u7F16\u53F7", width: 130, alignment: "left" }), _jsx(Column, { dataField: "KMMC", caption: "\u79D1\u76EE\u540D\u79F0", width: 200, alignment: "left" }), _jsx(Column, { dataField: "KMFX", caption: "\u79D1\u76EE\u65B9\u5411", width: 80, alignment: "center" }), _jsx(Column, { dataField: "QCYE", caption: "\u671F\u521D\u4F59\u989D", width: 120, alignment: "right", cellRender: (cell) => {
                                                    return cell.data.QCYE ? moneyFormat(cell.data.QCYE) : "";
                                                } }), _jsx(Column, { dataField: "JFLJFSE", caption: "\u501F\u65B9\u7D2F\u8BA1", width: 130, alignment: "right", cellRender: (cell) => {
                                                    return cell.data.JFLJFSE
                                                        ? moneyFormat(cell.data.JFLJFSE)
                                                        : "";
                                                } }), _jsx(Column, { dataField: "DFLJFSE", caption: "\u8D37\u65B9\u7D2F\u8BA1", width: 130, alignment: "right", cellRender: (cell) => {
                                                    return cell.data.DFLJFSE
                                                        ? moneyFormat(cell.data.DFLJFSE)
                                                        : "";
                                                } }), _jsx(Column, { dataField: "QMYE", caption: "\u671F\u672B\u4F59\u989D", width: 130, alignment: "right", cellRender: (cell) => {
                                                    return cell.data.QMYE ? moneyFormat(cell.data.QMYE) : "";
                                                } }), _jsx(Column, { dataField: "itemNames2", caption: "\u62A5\u8868\u9879\u76EE", width: 130, alignment: "left" })] })] })] }));
                } }), _jsx(DevPopup.Default, { ref: TreeAuxiliaryRef, children: () => {
                    //
                    return _jsx("span", { children: `${AddFZZKMMC}` });
                } }), _jsx(DevPopup.Default, { ref: typeRef, children: () => {
                    //
                    return (_jsx("span", { children: `【${curRowKMMC}】为末级科目，是否继续为其增加下级科目？` }));
                } }), _jsx(DevPopup.Default, { ref: typeErrorRef, children: () => {
                    return (_jsxs(_Fragment, { children: [_jsx("span", { children: `【${curRowKMMC}】` }), _jsx("span", { style: { color: "red" }, children: "\u4E3A\u672B\u7EA7\u79D1\u76EE\uFF0C\u5DF2\u88AB\u8BBE\u7F6E\u8F85\u52A9\u8D26\uFF0C\u65E0\u6CD5\u589E\u52A0\u4E0B\u7EA7\u79D1\u76EE!" })] }));
                } }), _jsx(DevPopup.Default, { ref: DeleteSubjectRef, children: () => {
                    //
                    return (_jsxs("div", { children: [_jsx("p", { children: "\u786E\u5B9A\u8981\u5220\u9664:" }), _jsx("p", { children: curRowKMMC })] }));
                } }), _jsx(SubjectsPopupData, { ref: SubjectPopupDataRef, AccountDataID: diaParams.current?.Data_ID, dbname: diaParams.current?.dbname, username: diaParams.current?.editUser }), _jsx(FirstLevelSubjectPopup, { ref: FirstLevelSubjectPopupRef, AccountDataID: diaParams.current?.Data_ID, dbname: diaParams.current?.dbname, username: diaParams.current?.editUser, zcbfid: diaParams.current?.dbname })] }));
});
export default React.memo(SubjectPopup);
